using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace buildingLib
{
    [Serializable()]
    public class Foreigner : BusinessBase<Foreigner>
    {
        #region Business Methods
        int m_id;
        string m_name = string.Empty;
        string m_ename = string.Empty;
        string m_certype = string.Empty;
        string m_cerid = string.Empty;
        bool m_gender = true;
        string m_nation = string.Empty;
        DateTime m_arrvialtime = DateTime.Now;
        DateTime m_certime = DateTime.Now;
        string m_memo = string.Empty;
        Guid m_doorid;

        public int ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }

        public string Name
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_name;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_name != value)
                {
                    m_name = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Ename
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_ename;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_ename != value)
                {
                    m_ename = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Certype
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_certype;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_certype != value)
                {
                    m_certype = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Cerid
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_cerid;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_cerid != value)
                {
                    m_cerid = value;
                    PropertyHasChanged();
                }
            }
        }
        public bool Gender
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_gender;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_gender != value)
                {
                    m_gender = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Nation
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_nation;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_nation != value)
                {
                    m_nation = value;
                    PropertyHasChanged();
                }
            }
        }
        public DateTime Arrvialtime
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_arrvialtime;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_arrvialtime != value)
                {
                    m_arrvialtime = value;
                    PropertyHasChanged();
                }
            }
        }
        public DateTime Certime
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_certime;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_certime != value)
                {
                    m_certime = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Memo
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_memo;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_memo != value)
                {
                    m_memo = value;
                    PropertyHasChanged();
                }
            }
        }
        public Guid Doorid
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_doorid;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_doorid != value)
                {
                    m_doorid = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return m_id;
        }
        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Name");
            //ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Ename");
            //ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Certype");
            //ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Cerid");
            //ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Nation");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        public static Foreigner NewForeigner(Guid doorid)
        {
            // TODO: change to use new keyword if not loading defaults
            return new Foreigner(doorid);
        }

        internal static Foreigner GetForeigner(SafeDataReader dr)
        {
            return new Foreigner(dr);
        }

        private Foreigner(Guid doorid)
        {
            m_doorid = doorid;
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        private Foreigner(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            m_id = dr.GetInt32("id");

            m_name = dr.GetString("name");
            m_ename = dr.GetString("ename");
            m_certype = dr.GetString("certype");
            m_cerid = dr.GetString("cerid");
            m_gender = dr.GetBoolean("gender");
            m_nation = dr.GetString("nation");
            m_arrvialtime = dr.GetDateTime("arrvialtime");
            m_certime = dr.GetDateTime("certime");
            m_memo = dr.GetString("memo");
            MarkOld();
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AInsertForeigner");
            db.AddOutParameter(dbcom, "id", DbType.Int32, m_id);
            db.AddInParameter(dbcom, "doorid", DbType.Guid, m_doorid);
            DoInsertUpdate(dbcom);
            m_id = (Int32)db.GetParameterValue(dbcom, "id");
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AUpdateForeigner");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);

            DoInsertUpdate(dbcom);
            MarkOld();
        }

        private void DoInsertUpdate(DbCommand dbcom)
        {
            Database db = SQLDatabase.GetDatabase();
            db.AddInParameter(dbcom, "name", DbType.String, m_name);
            db.AddInParameter(dbcom, "ename", DbType.String, m_ename);
            db.AddInParameter(dbcom, "certype", DbType.String, m_certype);
            db.AddInParameter(dbcom, "cerid", DbType.String, m_cerid);
            db.AddInParameter(dbcom, "gender", DbType.Boolean, m_gender);
            db.AddInParameter(dbcom, "nation", DbType.String, m_nation);
            db.AddInParameter(dbcom, "arrvialtime", DbType.DateTime, m_arrvialtime);
            db.AddInParameter(dbcom, "certime", DbType.DateTime, m_certime);
            db.AddInParameter(dbcom, "memo", DbType.String, m_memo);

            db.ExecuteNonQuery(dbcom);
        }

        internal void DeleteSelf()
        {
            // if we're not dirty then don't update the database
            if (!this.IsDirty) return;

            // if we're new then don't update the database
            if (this.IsNew) return;

            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetSqlStringCommand("delete from foreigner where id=@id");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);

            db.ExecuteNonQuery(dbcom);

            MarkNew();
        }

        #endregion
    }//end of class	
}//end of namespace 

